package service;

import java.util.ArrayList;

import org.apache.ibatis.session.SqlSession;

import bean.OrderRecipients;
import bean.Orders;
import bean.ShopItems;
import mapper.OrdersMapper;
import util.MybatisUtil;

public class OrdersServiceImpl implements OrdersService{

	@Override
	public ArrayList<Orders> getAllOrders() {
		
		SqlSession session = MybatisUtil.getSqlSession();
		OrdersMapper mapper = session.getMapper(OrdersMapper.class);
		ArrayList<Orders> allUsers = mapper.getAllOrders();
		session.commit();
		return allUsers;
	}

	@Override
	public ArrayList<Orders> getOrderStatusById(String orderId) {
		 SqlSession sqlSession = MybatisUtil.getSqlSession();
		 OrdersMapper orderMapper = sqlSession.getMapper(OrdersMapper.class);
		 ArrayList<Orders> ordersById = orderMapper.getOrderStatusById(orderId);
		 return ordersById;
	}

	@Override
	public ArrayList<Orders> getOrdersAndGoods(int orderUser) {
		 SqlSession sqlSession = MybatisUtil.getSqlSession();
		 OrdersMapper orderMapper = sqlSession.getMapper(OrdersMapper.class);
		 ArrayList<Orders> orders = orderMapper.getOrdersAndGoods(orderUser);	 
		 sqlSession.commit();
		return orders;
	}

	@Override
	public ArrayList<Orders> getOrderDetailByUsers(int orderUser) {
		SqlSession session = MybatisUtil.getSqlSession();
		OrdersMapper mapper = session.getMapper(OrdersMapper.class);
		int test = 1;
		ArrayList<Orders> list = mapper.getOrderDetailByUsers(test);
		session.commit();
		return list;
	}

	@Override
	public ArrayList<Orders> getPageOrdersByPageNum(int pageNum) {
		
			// TODO Auto-generated method stub
			SqlSession sqlSession = MybatisUtil.getSqlSession();
			OrdersMapper goodsMapper = sqlSession.getMapper(OrdersMapper.class);
			ArrayList<Orders> getAllOrders = goodsMapper.getAllOrders();
			return getAllOrders;
	}
	
		@Override
	public Orders getOrderStatusByUUId(String orderId) {
		SqlSession sqlSession = MybatisUtil.getSqlSession();
		OrdersMapper orderMapper = sqlSession.getMapper(OrdersMapper.class);
		Orders order = orderMapper.getOrderStatusByUUId(orderId);
		sqlSession.commit();
		return order;
	}

	@Override
	public int setNewOrders(String orderId, OrderRecipients orderRecipients, int userId, Double orderPrice) {
		//����һ���¶���
		SqlSession sqlSession = MybatisUtil.getSqlSession();
		OrdersMapper orderMapper = sqlSession.getMapper(OrdersMapper.class);
		orderMapper.setNewOrders(orderRecipients);
		sqlSession.commit();
		orderMapper.updatePrice(orderPrice);
		orderMapper.updateUUID(orderId);
		sqlSession.commit();
		return 1;
	}

	@Override
	public int setNewOrdersDetail(String orderId, ArrayList<ShopItems> shopItems) {
		//һ��������ȫ������ϸ��
		SqlSession sqlSession = MybatisUtil.getSqlSession();
		OrdersMapper orderMapper = sqlSession.getMapper(OrdersMapper.class);
		for(ShopItems item:shopItems){
			//����һ������ϸ��
			orderMapper.setNewOrderDetail(item);
			//�޸�ϸ�ڶ�����
			orderMapper.setOrderDetailId(orderId);
		}
		sqlSession.commit();
		return 1;
	}

	
}
